<?php
include "../database/database.php";

class CouponBean
{
	public $couponid;
	public $coutitle;
	public $couimage;
	public $couprice;
	public $isGet;
	public $shop;
	public $shopid;
	public $enddate;
	public $startdate;
	public $jianjie;
	public $top_price;
	public $type;
	public $fanwei;
	

	function __construct($couponId,$couTitle,$couImage,$couPrice,$isGet,$shop,$shopid,$enddate,$startdate,$jianjie,$top_price,$type,$fanwei)
	{
		$this->couponid = $couponId;
		$this->coutitle = $couTitle;
		$this->couimage = $couImage;
		$this->couprice = $couPrice;
		$this->isGet = $isGet;
		$this->shop = $shop;
		$this->shopid = $shopid;
		$this->enddate = $enddate;
		$this->startdate = $startdate;
		$this->jianjie = $jianjie;
		$this->top_price = $top_price;
		$this->type = $type;
		$this->fanwei = $fanwei;
	}
}

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end

//优惠券类型，1 新人专享优惠券，2 普通平台优惠券，3 商家优惠券
$today = date("Y-m-d");
$sql = "SELECT * FROM  coupon  where coupon.type=".$_REQUEST[type]."   and coupon.state=1  order by couponid  desc  limit $de,$size";
$result = $conn->query($sql);
$couPon = array();

if ($result->num_rows > 0 ) {
	while ($row = $result->fetch_assoc()) {
	     $num = $row['number'];
	     $shopid = $row['shopid'];
		if ($num != 0) {
			//判断用户是否领取优惠券
			$sql1="select * from mycoupon where userid='".$_REQUEST[userid]."' and couponid='".$row[couponid]."'";
			$result1=$conn->query($sql1);
			if ($result1->num_rows>0) {
				$isGet=1;//1为已经领取
			}else{
				$isGet=0;//0为未领取
			}
			//判断用户是否领取优惠券 end
			
			$shop=0;
			$categroyids = $row[categroyids];
			//获取该优惠券的商户名
			if($shopid!=0){
                $sql2="select * from shop where shopid=".$shopid;
				$result2=$conn->query($sql2);
				$row2 = $result2->fetch_assoc();
				if($row2){
				   $shop = $row2[name];
				   $categroyids = $row2[leimu];
				 }
			}

			//获取分类名称
			$cateNames = array();
			$categroyidsArray = explode(",", $categroyids);
			//平台优惠券
			if(in_array("0",$categroyidsArray)){
				$cateNames[] = '任意';
			}else{
                $sql2="select name from categroy where cateid in (".$categroyids.")";
				$result2=$conn->query($sql2);
				if($result2){
					while ($row2 = $result2->fetch_assoc()) {
						$cateNames[] = $row2[name];
					}
				}
					
			}

			$couPon[] = new CouponBean($row['couponid'],$row['title'],$row['srcimages'],$row['price'],$isGet,$shop,$shopid,$row['enddate'],$row['startdate'],$row['jianjie'],$row['top_price'],$row['type'],$cateNames);
		}
	}
}

$conn->close();

echo json_encode(array('code' => 200,'msg' => "查询成功",'data' => $couPon ));
?>